import { defineStore } from 'pinia';
import { AppState } from './types';
import { setToken, clearToken, getToken } from '@/utils/auth';
import { userLogin, LoginData } from "@/api/draw";


const useAppStore = defineStore('app', {
    state: (): AppState => ({
        token: "",
    }),
    getters: {
        isLogin: (): boolean => {
            console.log(getToken() != '' && getToken() != null)
            return getToken() != '' && getToken() != null
        }
    },
    actions: {
        async login(loginForm: LoginData) {
            try {
                const { data } = await userLogin(loginForm);
                setToken(data.token);
            } catch (err) {
                clearToken();
                throw err;
            }
        },
    }
})


export default useAppStore;